﻿using System;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Book_Shop.BLL;
using Book_Shop.DAL.DAO;

namespace Book_Shop.UI
{
    public partial class SalesPersonUIMock : Page
    {
        private BookOperationManager bookOperationManager = new BookOperationManager();
        private OrderSheet orderSheet;
        private OrderManager orderManager = new OrderManager();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                messageLabel.Text = "";
                browseBookListBox.DataSource = bookOperationManager.GetAll();
                browseBookListBox.DataTextField = "Title";
                browseBookListBox.DataValueField = "Id";
                browseBookListBox.DataBind();
                
            }
        }

        protected void newSaleButton_Click(object sender, EventArgs e)
        {
            string message;
            message =  orderManager.InitOrder(ref orderSheet);
            messageLabel.ForeColor = Color.Green;
            messageLabel.Text = message;
            Session["CurrentOrderSheet"] = orderSheet;
        }

        protected void goToOrderSheetButton_Click(object sender, EventArgs e)
        {
            orderSheet = (OrderSheet)Session["CurrentOrderSheet"];
            if (orderSheet!=null)
            {
               Response.Redirect("~/UI/OrderSheet.aspx");
            }
            else
            {
                messageLabel.ForeColor = Color.Red;
                messageLabel.Text = "No Order Sheet Created!";
            }
        }

        protected void addBookButton_Click(object sender, EventArgs e)
        {
            orderSheet = (OrderSheet)Session["CurrentOrderSheet"];
            try
            {
                if (browseBookListBox.SelectedIndex!=-1)
                {
                    Book book = bookOperationManager.GetById(int.Parse(browseBookListBox.SelectedValue));
                    messageLabel.Text = orderManager.AddBookToOrderSheet(orderSheet, book);
                    
                }
                else
                {
                    messageLabel.ForeColor = Color.Red;
                    messageLabel.Text = "Please Select a Book at first then try to add it.";
                }
                
            }
            catch (Exception exception)
            {
                messageLabel.ForeColor = Color.Red;
                messageLabel.Text = exception.Message;
            }
                
            
        }
    }
}